class Solution {
public:
    int findSubstringInWraproundString(string p) {
        int n = p.size();
        if(n <= 1) return n;
      
        vector<int> dp(26,0);
        dp[p[0]-'a'] = 1;
        int len = 1;
        int count = 0;
        
        for(int i = 1;i < n;i++){
            if(p[i] == p[i-1]+1 || (p[i-1] == 'z' && p[i] == 'a')){
                len++;
            }else{
                len = 1;
            }
            dp[p[i]-'a'] = max(dp[p[i]-'a'],len); 
        }
        
        for(int num : dp){
            count += num;
        }
        
        return count;
    }
};
